package com.atguigu.ggkt.wechat.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.ggkt.exception.GgktException;
import com.atguigu.ggkt.model.wechat.Menu;
import com.atguigu.ggkt.vo.wechat.MenuVo;
import com.atguigu.ggkt.wechat.mapper.MenuMapper;
import com.atguigu.ggkt.wechat.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author sleephardzz
 * @since 2023-07-23
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private WxMpService wxMpService;

    ////公众号菜单删除
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new GgktException(20001,"删除公众号菜单异常");
        }
    }

    //同步菜单方法：【JsonArray 和 jsonObject】
    @Override
    public void syncMenu() {
        //获取所有菜单
        List<MenuVo> menuVoList = this.findMenuInfo();
        //封装button内格式
        JSONArray button = new JSONArray();
        for (MenuVo oneMenuVo:menuVoList) {
                //包括第一层第二层
            //第一层 json对象
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            //第二层：json数组
            JSONArray sub_button = new JSONArray();
            for (MenuVo twoMenuVo:oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type", twoMenuVo.getType());
                if(twoMenuVo.getType().equals("view")) {
                    view.put("name", twoMenuVo.getName());
                    view.put("url", "http://ggkt2.vipgz1.91tunnel.com/#"
                            +twoMenuVo.getUrl());
                } else {
                    view.put("name", twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                sub_button.add(view);
            }
            one.put("sub_button", sub_button);
            button.add(one);
        }
        //封装最外层：button对象
        JSONObject buttonObject = new JSONObject();
        buttonObject.put("button", button);
        //传入想要的菜单结构样式
        try {
           //返回菜单id
            String menuId = this.wxMpService.getMenuService().menuCreate(buttonObject.toJSONString());
            System.out.println("返回菜单id  = "+menuId);
        } catch (WxErrorException e) {
            throw new GgktException(20001, "返回菜单id失败");
        }
    }



    //重要方法：获取所有一级菜单
    @Override
    public List<Menu> findOneMenuInfo() {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        /** 0为一级菜单 */
        wrapper.eq(Menu::getParentId,0);
        List<Menu> menuList = baseMapper.selectList(wrapper);
        return menuList;
    }

    //重要方法：获取所有菜单，按照一级和二级进行封装
    @Override
    public List<MenuVo> findMenuInfo() {
        //1 list集合
        ArrayList<MenuVo> resList = new ArrayList<>();
        //2 查询菜单数据---包含一级和二级
        List<Menu> allMenuList = baseMapper.selectList(null);
        //3 从所有菜单获取所有一级菜单数据（parent_id = 0）
        List<Menu> oneMenuList = allMenuList.stream()
                .filter(menu -> menu.getParentId().longValue() == 0)/** 为0的才封装 */
                .collect(Collectors.toList());
        //4 封装一级菜单数据，封装到list 【泛型集合不一样，遍历一级oneList封装到resList】
        for (Menu oneMenu:oneMenuList) {
                //Menu --> MenVo
            MenuVo oneMenuVo = new MenuVo();
            BeanUtils.copyProperties(oneMenu, oneMenuVo);

            //5 封装二级菜单【需要一级菜单 和 二级菜单parent_id 相同】
            List<Menu> twoMenuList = allMenuList.stream()
                    /** 一级id  = 二级parentid  */
                    .filter(twoMenu -> twoMenu.getParentId().longValue() == oneMenu.getId())
                    .collect(Collectors.toList());
            ArrayList<MenuVo> children = new ArrayList<>();
            for (Menu twoMenu:twoMenuList) {
                //List<Menu> --> List<MenuVo>
                MenuVo twoMenuVo = new MenuVo();
                BeanUtils.copyProperties(twoMenu, twoMenuVo);
                children.add(twoMenuVo);
            }
            //设置入属性中
            oneMenuVo.setChildren(children);
            //结果放入
            resList.add(oneMenuVo);
        }
        return resList;
    }

}
